package com.idealighter.game.games.doudizhu.struct;

import static com.idealighter.game.games.doudizhu.struct.CallType.CALL_LANDLORD;
import static com.idealighter.game.games.doudizhu.struct.CallType.NOT_CALL_LANDLORD;
import static com.idealighter.game.games.doudizhu.struct.CallType.NOT_GRAB_LANDLORD;

import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.doudizhu.manager.DouDiZhuMgr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 斗地主桌子.
 * 
 * @date 2016年2月24日 上午11:36:53
 *
 */
public class DouDiZhuTable extends AbstractTable {

  public static final int CEDITS = 1000;

  // 座位数量
  public static final int SEAT_NUM = 3;
  // 所属房间
  public final DouDiZhuMatch match;
  // 牌桌的座位
  public final List<DouDiZhuAiSeat> seats = new ArrayList<>(SEAT_NUM);
  // 一副牌
  public final List<DouDiZhuCard> cards = Arrays.asList(DouDiZhuCard.values());
  // 下一个应该叫牌玩家位置,0代表第一个为位置
  public int nextCallOrder = -1;
  // 下一个应该出牌的玩家位置
  public int nextPlayOrder = -1;
  // 上一次出牌的玩家位置
  public int prePlayOrder = -1;
  // 上一次玩家出的牌
  public List<DouDiZhuCard> prePlayCards = null;
  // 上一次玩家出的牌的类型
  public DouDiZhuCardsType prePlayCardsType = null;
  // 游戏的倍数
  public int multiple = 1;
  // 游戏的底分
  public int baseCedits = 0;

  // 预赛是否结束
  public boolean preliminaryOver = false;
  // 本桌完成决赛轮数
  public int completedFinalsRuns = 0;


  /**
   * 构造函数.
   * 
   * @param id .
   * @param match .
   */
  public DouDiZhuTable(int id, DouDiZhuMatch match) {
    super(id);
    this.match = match;
  }


  /**
   * 下一个位置order .
   * 
   * @param seat 座位信息.
   * @return
   */
  public int nextSeatOrder(DouDiZhuAiSeat seat) {

    return (seat.getOrder() + 1) % seats.size();
  }

  /**
   * 下一个位置 .
   * 
   * @param seat 座位信息.
   * @return
   */
  public DouDiZhuAiSeat nextSeat(DouDiZhuAiSeat seat) {
    return seats.get(nextSeatOrder(seat));
  }

  /**
   * 上一个位置 .
   * 
   * @param seat 座位信息.
   * @return
   */
  public DouDiZhuAiSeat preSeat(DouDiZhuAiSeat seat) {
    return seats.get((seat.getOrder() + 2) % seats.size());
  }

  /**
   * 获取牌桌的地主位置.
   * 
   * @return
   */
  public DouDiZhuAiSeat landlord() {
    for (DouDiZhuAiSeat seat : seats) {
      if (seat.landlord) {
        return seat;
      }
    }

    return null;
  }

  /**
   * 是否所有人都没有叫地主.
   * 
   * @return
   */
  public boolean allNotCallLandlord() {
    // 是否所有人都没有叫牌
    boolean allNotCallCard = true;
    for (DouDiZhuAiSeat s : seats) {
      if (s.callType != NOT_CALL_LANDLORD) {
        allNotCallCard = false;
        break;
      }
    }

    return allNotCallCard;
  }

  /**
   * 下一个应该叫牌的seat .
   * 
   * @param seat 座位信息.
   * @return null:叫牌结束 .
   */
  public DouDiZhuAiSeat nextCallCardSeat(DouDiZhuAiSeat seat) {
    DouDiZhuAiSeat referSeat = seat;
    for (int i = 0; i < 2; i++) {
      DouDiZhuAiSeat preSeat = preSeat(referSeat);
      DouDiZhuAiSeat nextSeat = nextSeat(referSeat);

      if (nextSeat.callType == null) {
        return nextSeat;
      } else if (nextSeat.callType == CALL_LANDLORD) {
        if ((preSeat.callType == NOT_CALL_LANDLORD && seat.callType == NOT_CALL_LANDLORD)
            || (preSeat.callType == NOT_GRAB_LANDLORD && seat.callType == NOT_GRAB_LANDLORD)) {
          return null;
        } else {
          return nextSeat;
        }
      }

      referSeat = nextSeat;
    }

    return null;
  }

  /**
   * 是否加倍结束 .
   * 
   * @return
   */
  public boolean doubledOver() {
    for (DouDiZhuAiSeat seat : seats) {
      if (seat.doubled == null) {
        return false;
      }
    }

    return true;
  }

  /**
   * 是否准备结束,人数少于SEAT_NUM,没有准备好 .
   */
  public boolean readyOver() {
    if (seats.size() < SEAT_NUM) {
      return false;
    }

    for (DouDiZhuAiSeat seat : seats) {
      if (!seat.ready) {
        return false;
      }
    }

    return true;
  }

  /**
   * 是否有真实玩家 .
   * 
   * @return
   */
  public boolean hasRealPlayer() {
    for (DouDiZhuAiSeat s : seats) {
      if (!s.isRobot()) {
        return true;
      }
    }

    return false;
  }

  /**
   * 重置牌桌 .
   */
  public void reset() {
    this.nextCallOrder = -1;
    this.nextPlayOrder = -1;
    this.prePlayOrder = -1;
    this.prePlayCards = null;
    this.prePlayCardsType = null;
    this.multiple = 1;
    this.baseCedits = 0;
  }

  /**
   * 牌桌的底牌,最后三张 .
   * 
   * @return
   */
  public List<DouDiZhuCard> hiddenCards() {

    return cards.subList(cards.size() - DouDiZhuMgr.HIDDEN_CARDS, cards.size());
  }

  @SuppressWarnings("unchecked")
  @Override
  public DouDiZhuRoom room() {
    return match.room;
  }

  @Override
  public List<DouDiZhuAiSeat> seats() {
    return seats;
  }
}
